package com.example.backend.controller;


import com.example.backend.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @Description:
 * @ClassName: Register
 * @Author: mobiusband
 * @Date:2022/12/5 22:37
 * @Version: 1.0
 */
@RestController
@CrossOrigin
public class Register {

    final static String USER = "regiestUser";

    @Autowired
    private RedisTemplate redisTemplate;

    //    用户注册接口
    @PostMapping("/register")
    @Transactional
    public String regiestUser(@RequestBody User user) {
        System.out.println("注册用user = {User@6512} 户" + user.toString());
        if (user.getName().isEmpty()){
            return "用户名不能为空";
        } if (user.getSex().isEmpty()){
            return "性别不能为空";
        } if (user.getAddr().isEmpty()){
            return "地址不能为空";
        }

//        存储到redis list里面
        redisTemplate.opsForList().rightPush(USER, user);
        System.out.println("hello@@@@@");
        return "注册成功";
    }

    //获取所有用户注册信息
    @GetMapping("/getUser")
    public Object getAllUser() {
        System.out.println("coming");
//        到redis获取用户信息
        List range = redisTemplate.opsForList().range(USER, 0, -1);

        System.out.println(range);
//        返回用户List
        return range;
    }
}
